﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dude.MLS;
using System.Xml.Serialization;
using System.ComponentModel;

namespace Dude.Data.Meta
{
    [Serializable]
    [Description("Information abou database field and Dude's mappings, description")]
    [XmlRoot(ElementName = "field")]
    public class DataFieldInfo : ITreeReconstruct, IVisitedDataModel
    {
        public DataFieldInfo()
        {
            Description = new MLString();
            DisplayName = new MLString();
            LongDescription = new MLString();
        }

        [Category("General")]
        [Description("Name that Dude uses for reference the field")]
        [XmlAttribute("name")]
        public string FieldName { get; set; }

        [Category("General")]
        [Description("Name of field in database for database queries")]
        [XmlAttribute("dbname")]
        public string DBFieldName { get; set; }

        [Category("Localization")]
        [Description("Name that the user see on grids, forms...")]
        [XmlElement("display")]
        public MLString DisplayName { get; set; }

        [Category("Localization")]
        [Description("Description of field meaning")]
        [XmlElement("desc")]
        public MLString Description { get; set; }

        [Category("Localization")]
        [Description("Long description of field meaning, should describe meaning or suggestions for data they contains")]
        [XmlElement("longdesc")]
        public MLString LongDescription { get; set; }

        [Category("Type info")]
        [Description("database type name - int, numeric, auto, varchar(100), text,... See database manual")]
        [XmlAttribute("dbtype")]
        public string DBTypeName { get; set; }

        private string typeName;
        [Category("Type info")]
        [Description(".NET system name - System.String, System.Decimal, System.Int64, System.Int32, System.Boolean, ...")]
        [DisplayName(".NET type name")]
        [XmlAttribute("type")]
        public string TypeName 
        {
            get
            {
                return typeName;
            }
            set
            {
                typeName = value;
                dataType = null;
            }
        }

        private Type dataType;
        [Browsable(false)]
        [XmlIgnore]
        public Type DataType 
        {
            get
            {
                if (dataType == null)
                {
                    dataType = Type.GetType(typeName, true);
                }
                return dataType;
            }
            set
            {
                dataType = value;
                TypeName = value.FullName;
            }
        }

        public override string ToString()
        {
            return String.Format("{0}: {1} - {2}", FieldName, TypeName, DisplayName.ToString());
        }

        [XmlIgnore]
        [Browsable(false)]
        public TableInfo Table { get; set; }

        #region ITreeReconstruct Members

        public void ReconstructTree(object parent)
        {
            Table = (TableInfo)parent;
        }

        #endregion

        #region IVisitedDataModel Members

        public void VisitedDataModel(IDataModelVisitor visitor)
        {
            visitor.VisitFieldInfo(this);
        }

        #endregion
    }
}
